//
// Created by tang on 2022/1/20.
//

#include "utils/app_utils.hpp"
#include "app/app_base.hpp"
#include "event/events.hpp"
#include "fmt/format.h"

using namespace AppEvent;

void SendDebug(const std::string& msg, const std::source_location& loc) {
#if !defined(NDEBUG)
    App->Bus()->postpone(DialogMessage{ DialogMessage::eDebug, fmt::format("{}\n\nSEND BY:\n{} @ {}:{}", msg, loc.function_name(), loc.file_name(), loc.line()) });
#else
    App->Bus()->postpone(DialogMessage{ DialogMessage::eDebug, fmt::format("{}", msg) });
#endif
}

void SendInfo(const std::string& msg, const std::source_location& loc) {

    // App->Bus()->postpone(DialogMessage{DialogMessage::eInfo, fmt::format("{}\n\nSEND BY:\n{} @ {}:{}", msg, loc.function_name(), loc.file_name(), loc.line())});
#if !defined(NDEBUG)
    App->Bus()->postpone(DialogMessage{ DialogMessage::eDebug, fmt::format("{}\n\nSEND BY:\n{} @ {}:{}", msg, loc.function_name(), loc.file_name(), loc.line()) });
#else
    App->Bus()->postpone(DialogMessage{ DialogMessage::eInfo, fmt::format("{}", msg) });
#endif
}

void SendWarning(const std::string& msg, const std::source_location& loc) {

    // App->Bus()->postpone(DialogMessage{DialogMessage::eWarning, fmt::format("{}\n\nSEND BY:\n{} @ {}:{}", msg, loc.function_name(), loc.file_name(), loc.line())});
#if !defined(NDEBUG)
    App->Bus()->postpone(DialogMessage{ DialogMessage::eWarning, fmt::format("{}\n\nSEND BY:\n{} @ {}:{}", msg, loc.function_name(), loc.file_name(), loc.line()) });
#else
    App->Bus()->postpone(DialogMessage{ DialogMessage::eWarning, fmt::format("{}", msg) });
#endif
}

void SendError(const std::string& msg, const std::source_location& loc) {

    // App->Bus()->postpone(DialogMessage{DialogMessage::eError, fmt::format("{}\n\nSEND BY:\n{} @ {}:{}", msg, loc.function_name(), loc.file_name(), loc.line())});
#if !defined(NDEBUG)
    App->Bus()->postpone(DialogMessage{ DialogMessage::eError, fmt::format("{}\n\nSEND BY:\n{} @ {}:{}", msg, loc.function_name(), loc.file_name(), loc.line()) });
#else
    App->Bus()->postpone(DialogMessage{ DialogMessage::eError, fmt::format("{}", msg) });
#endif
}

void ShowProgressDialog(bool open)
{
    App->Bus()->postpone(DialogMessage{ .type = DialogMessage::eProgressDialog, .show = open });
}

void SendNotification(const std::string& text, double timeout_ms) {
    App->Bus()->postpone(StatusMessage{ text, timeout_ms, 0, {} });
}

void SendNotificationEx(const std::string& text, double timeout_ms, std::string n1, notify_cb f1, std::string n2,
    notify_cb f2, std::string n3, notify_cb f3)
{
    StatusMessage msg{ text, timeout_ms, 0, {} };
    if (!n1.empty() && f1)
    {
        msg.operations[msg.op_cnt].name = move(n1);
        msg.operations[msg.op_cnt].cb = move(f1);
        ++msg.op_cnt;
    }
    if (!n2.empty() && f2)
    {
        msg.operations[msg.op_cnt].name = move(n2);
        msg.operations[msg.op_cnt].cb = move(f2);
        ++msg.op_cnt;
    }
    if (!n3.empty() && f3)
    {
        msg.operations[msg.op_cnt].name = move(n3);
        msg.operations[msg.op_cnt].cb = move(f3);
        ++msg.op_cnt;
    }
    App->Bus()->postpone(msg);
}

void ShowStatistics(const std::string &start, const std::string &end)
{
    App->Bus()->postpone(AppEvent::ShowStatisticsByRangeEvent{.start = start, .end = end});
}

void CopyText(const std::string &text) {
    App->Bus()->postpone(AppEvent::CopyTextEvent{text});
}

void CreateWorkItem(const std::string &date, const std::string &title, double hours)
{
    App->Bus()->postpone(AppEvent::CreateDiaryItemEvent{
        .date = date,
        .title = title,
        .note = {},
        .issue_id = -1,
        .act_id = -1,
        .time = hours,
        .tags = {},
    });
}

void CreateWorkItemWithNote (const std::string &date, const std::string &title, double hours, const std::string &note) {
    App->Bus()->postpone(AppEvent::CreateDiaryItemEvent{
        .date = date,
        .title = title,
        .note = note,
        .issue_id = -1,
        .act_id = -1,
        .time = hours,
        .tags = {},
    });
}
